package cn.wolfcode.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.Customer;
import cn.wolfcode.business.mapper.CustomerMapper;
import cn.wolfcode.common.exception.ServiceException;
import cn.wolfcode.common.utils.DateUtils;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.VisitMapper;
import cn.wolfcode.business.domain.Visit;
import cn.wolfcode.business.service.IVisitService;

/**
 * 拜访记录Service业务层处理
 * 
 * @author dalang
 * @date 2023-11-05
 */
@Service
public class VisitServiceImpl implements IVisitService 
{
    @Autowired
    private VisitMapper visitMapper;

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询拜访记录
     * 
     * @param id 拜访记录主键
     * @return 拜访记录
     */
    @Override
    public Visit selectVisitById(Long id)
    {
        return visitMapper.selectVisitById(id);
    }

    /**
     * 查询拜访记录列表
     * 
     * @param visit 拜访记录
     * @return 拜访记录
     */
    @Override
    public List<Visit> selectVisitList(Visit visit)
    {
        return visitMapper.selectVisitList(visit);
    }

    /**
     * 新增拜访记录
     * 
     * @param visit 拜访记录
     * @return 结果
     */
    @Override
    public int insertVisit(Visit visit)
    {
        if (visit==null) {
            throw new ServiceException("参数错误");
        }

        boolean timeFlag = visit.getVisitTime().before(DateUtils.getNowDate());
        if (!timeFlag) {
            throw new ServiceException("回访时间不能大于当前时间");
        }

        Customer customer = customerMapper.selectCustomerById(visit.getCustomerId());
        if (customer==null) {
            throw new ServiceException("参数错误");
        }
        visit.setCustomerName(customer.getCustomerName());
        visit.setEditId(SecurityUtils.getUserId());
        visit.setEditName(SecurityUtils.getUsername());
        visit.setEditTime(new Date());

        return visitMapper.insertVisit(visit);
    }

    /**
     * 修改拜访记录
     * 
     * @param visit 拜访记录
     * @return 结果
     */
    @Override
    public int updateVisit(Visit visit)
    {
        return visitMapper.updateVisit(visit);
    }

    /**
     * 批量删除拜访记录
     * 
     * @param ids 需要删除的拜访记录主键
     * @return 结果
     */
    @Override
    public int deleteVisitByIds(Long[] ids)
    {
        return visitMapper.deleteVisitByIds(ids);
    }

    /**
     * 删除拜访记录信息
     * 
     * @param id 拜访记录主键
     * @return 结果
     */
    @Override
    public int deleteVisitById(Long id)
    {
        return visitMapper.deleteVisitById(id);
    }
}
